<template>
  <div class="login_page">
    <CommonHeader title="密码登录" :isBack="true" @back="goPage('back')" />
    <!-- 登录页主体 -->
    <div class="login_content page_no_tab_bar">
      <!-- 标题 -->
      <div class="login_title">欢迎回来</div>
      <!-- 账号 -->
      <div class="input_box">
        <div class="input_label">邮箱</div>
        <div class="input">
          <input
            type="text"
            placeholder="请输入您的邮箱"
            v-model="ruleForm.email"
          />
        </div>
        <div class="input_rule">{{ rules.email.rule }}</div>
      </div>
      <!-- 密码 -->
      <div class="input_box">
        <div class="input_label">密码</div>
        <div class="input input_psw">
          <input
            :type="isViewPsw ? 'text' : 'password'"
            placeholder="请输入您的的密码"
            v-model="ruleForm.password"
          />
          <div class="is_open" @click="viewPsw">
            <img v-if="!isViewPsw" src="@/assets/login/close_icon.png" alt="" />
            <img v-else src="@/assets/login/open_icon.png" alt="" />
          </div>
        </div>
        <div class="input_rule">{{ rules.password.rule }}</div>
      </div>
      <!-- 去注册 -->
      <div class="font_color" @click="goPage('/register')">没有账号？</div>
      <!-- 登录按钮 -->
      <div class="operate_btn" @click="goPage('/home')">登录</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import CommonHeader from "@/components/CommonHeader.vue";
import { useRouter } from "vue-router";
import { login } from "@/api/user";
import { ElMessage } from "element-plus";
import { useUserInfo } from "@/stores/userInfo";

// 获取路由对象
const router = useRouter();
// 获取用户信息
const user = useUserInfo();
// 密码是否可见
const isViewPsw = ref(false);
// 表单数据
const ruleForm: any = ref({
  email: "",
  password: "",
});
// 表单验证
const rules: any = reactive({
  email: {
    msg: "请输入邮箱",
    rule: "",
  },
  password: {
    msg: "请输入密码",
    rule: "",
  },
});

// 查看密码
const viewPsw = () => {
  isViewPsw.value = !isViewPsw.value;
};

// 跳转指定页
const goPage = (path: string) => {
  if (path == "/home") {
    if (validateForm()) {
      // 登录
      login(ruleForm.value).then((res: any) => {
        user.setUserInfo(res.data);
        localStorage.setItem("token", res.data.simuToken);
        router.push("home");
        ElMessage.success("登录成功！");
      });
    }
  } else if (path == "back") {
    router.push("home");
  } else {
    // 注册
    router.push(path);
  }
};

// 表单验证
const validateForm = () => {
  for (let k in rules) {
    // 验证是否为空
    if (ruleForm.value[k].trim() == "") {
      rules[k].rule = rules[k].msg;
      return false;
    }
  }
  for (let k in rules) {
    rules[k].rule = "";
  }
  return true;
};
</script>

<style lang="scss" scoped>
.login_page {
  .login_content {
    padding-top: 10px;
    .login_title {
      font-size: 22px;
      margin-bottom: 20px;
    }
    .input_box {
      // height: 108px;
      margin-bottom: 10px;
      .input {
        width: 100%;
        height: 50px;
        background-color: #141515;
        margin: 5px 0;
        input {
          width: 100%;
          height: 100%;
          font-size: 18px;
          padding: 0 5px;
          background-color: #141515;
          color: #ffffff;
        }
      }
      .input_psw {
        display: flex;
        align-items: center;
        input {
          flex: 1;
        }
        .is_open {
          display: flex;
          align-items: center;
          padding: 0px 10px;
        }
      }
      .input_rule {
        color: #e63737;
        font-size: 14px;
      }
    }
    .operate_btn {
      margin-top: 20px;
      background: linear-gradient(to right, #14da62, #0d5eae);
      text-align: center;
      padding: 10px;
      border-radius: 10px;
    }
  }
}
</style>
